const { getRoleSql, amendRoleStatusSql, getSearchSql, removeRoleSql, insertRoleSql,amendRoleSql } = require('../../model/role/index')
//获取role数据
const getRole = async function (req, res) {
  let data = await getRoleSql();
  res.send(data)
}
//修改role角色状态
const amendRoleStatus = async function (req, res) {
  let { id, roleStatus } = req.query;
  id = Number(id)
  roleStatus = roleStatus == 'false' ? 0 : 1
  console.log(id, roleStatus);
  await amendRoleStatusSql([roleStatus, id]);
  res.send({ code: 200 })
}
//搜索角色名称
const getSearch = async function(req,res){
  let {name} = req.query;
  let data = await getSearchSql([name]);
  res.send(data)
}
//删除
const removeRole = async function(req,res){
  let {id} = req.query;
  id = Number(id)
  await removeRoleSql([id]);
  res.send({code:200})
}
//添加
const insertRole = async function(req,res){
  let { name, roleIdentification, roleRank, rolePrivileges } = req.body;
  rolePrivileges = JSON.stringify(rolePrivileges)
  await insertRoleSql([name, roleIdentification, roleRank, rolePrivileges]);
  res.send({code:200})
}
//修改
const amendRole = async function(req,res){
  let { name, roleIdentification, roleRank, rolePrivileges, roleId } = req.body;
  rolePrivileges = JSON.stringify(rolePrivileges)
  await amendRoleSql([name, roleIdentification, roleRank, rolePrivileges, roleId]);
  res.send({code:200})
}
module.exports = {
  getRole,
  amendRoleStatus,
  getSearch,
  removeRole,
  insertRole,
  amendRole
}